<?php

interface Formatter
{
	public function Execute($e);
}

class BaseError extends Exception
{

	public $formatter = null;
	
	public function __construct($ErrorMessage)
	{
		parent::__construct($ErrorMessage, 0);
	}
	
	public function __toString()
	{
		if(is_null($this->formatter))
		{
			return $this->getMessage();
		}
		else
		{
			return $this->formatter->Execute($this);
		}
	}
}

class exceptionErrorHandler
{

	public $error_formatter = null;

	public function __construct()
	{
		//error_reporting(FATAL | ERROR | WARNING);
		set_error_handler(array ($this, 'errorHandler'));
	}

	public function errorHandler ($ErrorCode, $ErrorMessage, $ErrorFile, $ErrorLine, $Vars)
	{
		$e = new BaseError($ErrorMessage);
		$e->formatter = $this->error_formatter; 
		throw $e;
	}
}

class SimpleFormatter implements Formatter
{
	public function Execute($e)
	{
		$msg = $e->getMessage()."\n";
		$msg .= $e->getTraceAsString();
		return $msg;
	}
}

class HtmlFormatter implements Formatter
{
	public function Execute($e)
	{
		$msg = "<b>".$e->getMessage()."</b>";
		$msg .= $e->getTraceAsString();
		$msg = str_replace("#", "<br/>#", $msg)."<br/>";
		return $msg;
	}
}
